<template>
  <div class="chart-container">
    <!-- 饼状图容器 -->
    <div ref="pieChart" class="chart-item"></div>
    <!-- 柱状图容器 -->
    <div ref="barChart" class="chart-item"></div>
    <!-- 折线图容器 -->
    <div ref="lineChart" class="chart-item"></div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';

const pieChart = ref(null);
const barChart = ref(null);
const lineChart = ref(null);

onMounted(() => {
  // 初始化饼状图
  const pieMyChart = echarts.init(pieChart.value);
  const pieOption = {
    title: {
      text: 'ECharts 饼状图示例',
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: '搜索引擎' },
          { value: 735, name: '直接访问' },
          { value: 580, name: '邮件营销' },
          { value: 484, name: '联盟广告' },
          { value: 300, name: '视频广告' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };
  pieMyChart.setOption(pieOption);

  // 初始化柱状图
  const barMyChart = echarts.init(barChart.value);
  const barOption = {
    title: {
      text: 'ECharts 柱状图示例',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['搜索引擎', '直接访问', '邮件营销', '联盟广告', '视频广告']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '访问量',
        type: 'bar',
        data: [1048, 735, 580, 484, 300]
      }
    ]
  };
  barMyChart.setOption(barOption);

  // 初始化折线图
  const lineMyChart = echarts.init(lineChart.value);
  const lineOption = {
    title: {
      text: 'ECharts 折线图示例',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['搜索引擎', '直接访问', '邮件营销', '联盟广告', '视频广告']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '访问量',
        type: 'line',
        data: [1048, 735, 580, 484, 300]
      }
    ]
  };
  lineMyChart.setOption(lineOption);
});
</script>

<style scoped>
.chart-container {
  display: flex;
  gap: 10px; /* 设置图表间的间隙为 10px */
  justify-content: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  background: #f8f8f8;
  opacity: 0.8;
}

.chart-item {
  flex: 0 0 calc(33.333% - 6.667px); /* 每个图表宽度占三分之一，减去间隙 */
  min-width: 600px;
  height: 500px;
}
</style>